package alone.redis.queue.redisqueue.service;

import alone.redis.queue.redisqueue.util.RedisUtil;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;


/**
 * redis非阻塞以及阻塞方式生产者
 */
@Service
public class MessageProducerService extends Thread{

    private static final String keyName = "redis:queue";

//    @Resource
//    private StringRedisTemplate stringRedisTemplate;

    private void putMessage(String message){
        Jedis jedis = RedisUtil.getJedis();
        jedis.lpush(keyName,message);
    }

    @Override
    public void run() {
        //这里写4是因为redisPool的maxTotal为8
        for(int i=0;i<4;i++){
            System.err.println(Thread.currentThread().getName() + " " + i);
            putMessage(Thread.currentThread().getName() + i);
        }
    }

    /**
     * 双线程向队列中插入任务
     */
    public static void main(String[] args){
        MessageProducerService messageProducerService = new MessageProducerService();
        Thread thread1 = new Thread(messageProducerService,"111");
        Thread thread2 = new Thread(messageProducerService,"222");
        thread1.start();
        thread2.start();
    }



}
